package com.MarkToTop.main;

import com.MarkToTop.util.Constant;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * MarkWang 版权所有 © Copyright 2020
 *
 * @CreateDate: Created in 2020-06-22 08:21
 * @Author: MarkToTop -(づ｡◕‿‿◕｡)づ
 * @Description:   云的景儿， 在屏幕上方飘动  (一片云彩类)
 */
public class Cloud {

    private BufferedImage imageCloud;
    private int speed;

    //云彩飘动的方法
    private int dir;
    public static final int DIR_LEFT = 1;  // 向左
    public static final int DIR_RIGHT = 2; // 向右

    //云彩的缩放比例 1-1.3倍
    private double scale;
    private int scaleIMG_W,scaleIMG_H;

    private int x,y;

    public Cloud(BufferedImage imageCloud, int dir, int x, int y) {
        this.imageCloud = imageCloud;
        this.speed = Constant.CLOUD_FLY_SPEED;
        this.dir = dir;
        this.x = x;
        this.y = y;
        //得到 1-2之间的随机double值
        scale = 1 + Math.random();
        //缩放后的图片大小
        scaleIMG_H = (int)(scale * imageCloud.getHeight());
        scaleIMG_W = (int)(scale * imageCloud.getWidth());
    }

    public void draw(Graphics g){

        //根据方向移动速度(向左移动)
        x = (dir == DIR_LEFT) ? x - speed : x + speed;
        g.drawImage(imageCloud,x,y,scaleIMG_W,scaleIMG_H,null);
    }


    /**
     * 根据飞行方向判断云彩是否飞出屏幕
     *      向左飞时， x < -imageCloud.getWidth()，跑出屏幕左边
     *      向右飞时， x > Constant.Frame_Width，跑出屏幕右边
     *
     *      飞出时，返回true，没飞出返回false
     */
    public boolean isOutFrame(){
        boolean result = false;
        if(dir == DIR_LEFT){
            if (x < -imageCloud.getWidth()){
                return true;
            }
        }else if (dir == DIR_RIGHT){
            if (x > Constant.Frame_Width){
                return true;
            }
        }
        return result;
    }

}
